/*
*******************************************************************************
* ansi c source code
* file name:
*	AWGN.c
* abstract:
*	to stimulate a AWGN channel  
* author:Fangming He
*******************************************************************************
*/


/*
*******************************************************************************
*                               include files
*******************************************************************************
*/
#include <stdio.h>
#include <time.h>
#include <math.h>
#include "../common/globaldef.h"
#include "../common/Random.h"
#include "../common/globalVai.h"


/*
*******************************************************************************
*                            function definition
*******************************************************************************
*/
/*
*******************************************************************************
* description:
*	to add a Gaussian noise on the OFDM symbol 
*  input :
*  SNR :  the ratio of the Siganl and the noise
*  *Symbol: the address of the transmitted OFDM symbol
*  output :
*  none
*  referenced
*  Random.c
* author:
*	guo liang       2006-05-30
* revision history:
*	guo liang       2006-05-30	original version
*******************************************************************************
*/

int AWGN(double SNR,struct Complex *Symbol)
{
	time_t t;
	int i;
	double variance;
	double temp[2];
	struct Complex tmp;
	t = time(0) + 138;         /* to genenrate a seed for RunRandGas ;138 is a random*/
	temp[0] = 0;
	temp[1] = 0;
	for (i = 0 ;i < 1024; i++)
	{
		temp[0] += Symbol[i+96].real * Symbol[i+96].real;
		temp[1] += Symbol[i+96].imag * Symbol[i+96].imag;
	}
	variance = 1/pow(10,SNR/10);
	variance /= 4096;
	variance /= 8;
	SetRandGas(t);    
	for (i = 0; i < (FFT_Size+CP_Length); i++)
	{
		tmp = RunRandGas1(0,variance);    /* to generate a 0 mean Complex Gassian noise */
		Symbol[i].real += tmp.real;
		Symbol[i].imag += tmp.imag;
	}
	return 1;
}

